/**
 * 改变 一下思路，两个数字的六种运算！
 * List<String[]>会出现很让人头疼的问题！！
 */

package com.tomhu.doservices;

import java.util.HashSet;
import java.util.Set;

public class JudgePointServices {

    String[] strs = new String[3];
    static String resembleStrs;
    String[] operates = new String[]{"add", "sub", "rsub", "mul", "div", "rdiv"};
    String currentOperate;
    double operatedNumber;
    Set<String> List = new HashSet<String>();
    double[] newArrays;

    public Set<String> doServices(double[] arrays, int size) {

        if (arrays.length == 1) {
            double i = Math.pow(10, -13);
            if (24 - arrays[0] <= i && 24 - arrays[0] >= -i) {
                resembleWithStrs(strs);
                List.add(resembleStrs);
                return null;
            } else {
                return null;
            }
        }
        for (int i = 0; i < arrays.length - 1; i++) {
            for (int j = i + 1; j < arrays.length; j++) {
                for (int operaterNumber = 0; operaterNumber < operates.length; operaterNumber++) {
                    currentOperate = operates[operaterNumber];
                    //处理strs的问题！！
                    if ("add".equals(currentOperate)) {
                        operatedNumber = arrays[i] + arrays[j];
                        newArrays = ExtractnewArrays(arrays, i, j, operatedNumber);
                        strs[3 - newArrays.length] = arrays[i] + " + " + arrays[j] + " = " + (arrays[i] + arrays[j]);
                    } else if ("sub".equals(currentOperate)) {
                        operatedNumber = arrays[i] - arrays[j];
                        newArrays = ExtractnewArrays(arrays, i, j, operatedNumber);
                        strs[3 - newArrays.length] = arrays[i] + " - " + arrays[j] + " = " + (arrays[i] - arrays[j]);
                    } else if ("rsub".equals(currentOperate)) {
                        operatedNumber = arrays[j] - arrays[i];
                        newArrays = ExtractnewArrays(arrays, i, j, operatedNumber);
                        strs[3 - newArrays.length] = arrays[j] + " - " + arrays[i] + " = " + (arrays[j] - arrays[i]);
                    } else if ("mul".equals(currentOperate)) {
                        operatedNumber = arrays[i] * arrays[j];
                        newArrays = ExtractnewArrays(arrays, i, j, operatedNumber);
                        strs[3 - newArrays.length] = arrays[i] + " * " + arrays[j] + " = " + (arrays[i] * arrays[j]);
                    } else if ("div".equals(currentOperate)) {
                        operatedNumber = arrays[i] / arrays[j];
                        newArrays = ExtractnewArrays(arrays, i, j, operatedNumber);
                        strs[3 - newArrays.length] = arrays[i] + " / " + arrays[j] + " = " + (arrays[i] / arrays[j]);
                    } else if ("rdiv".equals(currentOperate)) {
                        operatedNumber = arrays[j] / arrays[i];
                        newArrays = ExtractnewArrays(arrays, i, j, operatedNumber);
                        strs[3 - newArrays.length] = arrays[j] + " / " + arrays[i] + " = " + (arrays[j] / arrays[i]);
                    } else {
                        com.tomhu.util.Error.showError();
                    }
                    doServices(newArrays, newArrays.length);
                }
            }
        }
        if (arrays.length == 4 && List.size() > 0) {

            System.out.println("hello world");
            return List;
        }
        return null;
    }

    //这是取出数字后的数组！！
    public double[] stillInArray(double[] arrays, int indexOne, int indexTwo) {
        int size = arrays.length;
        if (size - 2 < 1) {
            return null;
        }
        double[] newArray = new double[size - 2];
        int i = 0;
        for (int j = 0; j < size; j++) {
            if (j == indexOne || j == indexTwo) {
                continue;
            } else {
                newArray[i++] = arrays[j];
            }
        }
        return newArray;
    }

    //这是一个重新组合的数组, 是一个控制器！！
    public double[] ExtractnewArrays(double[] arrays, int indexOne, int indexTwo, double operatedNumber) {
        int resembleLength = arrays.length - 1;
        double[] resembleArray = new double[resembleLength];
        double[] stillInArray = stillInArray(arrays, indexOne, indexTwo);

        //把运算的结果放在第一位，以后的括号处理一致可以优化。
        resembleArray[0] = operatedNumber;
        if (stillInArray != null) {
            for (int i = 0; i < stillInArray.length; i++)
                resembleArray[i + 1] = stillInArray[i];
        } else {
            return resembleArray;
        }
        return resembleArray;
    }

    public static void resembleWithStrs(String[] strs) {
        String tempString = "";
        if (strs == null) {
            return;
        }
        for (int i = 0; i < strs.length; i++) {
            tempString += strs[i] + "&";
        }
        resembleStrs = tempString;
    }
}
